﻿using System.Linq.Expressions;
using Inovout.Data.Domain;

namespace System.Web.Mvc.Html
{
    /// <summary>
    /// Widget-Tree
    /// </summary>
    public static class TreeExtensions
    {
        /// <summary>
        /// 使用树控件
        /// </summary>
        /// <typeparam name="TModel">模型类型</typeparam>
        /// <typeparam name="TKeyProperty">映射的树中主键类型</typeparam>
        /// <typeparam name="TContentProperty">映射的树中内容的模型属性类型</typeparam>
        /// <param name="html">HtmlHelper实例</param>
        /// <param name="keyPropertyAccessor">映射的树中主键，通常为“Id”</param>
        /// <param name="contentPropertyAccessor">映射的树中内容的模型属性名</param>
        /// <param name="selectedKeys">选中的Key数组</param>
        /// <param name="nullRender">模型为NULL，呈现方法</param>
        /// <param name="partialViewName">局部视图名称</param>
        /// <returns>IHtmlString实例</returns>
        public static IHtmlString Tree<TModel, TKeyProperty, TContentProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TKeyProperty>> keyPropertyAccessor,
            Expression<Func<TModel, TContentProperty>> contentPropertyAccessor,string[] selectedKeys, Func<object> nullRender = null, string partialViewName = "_Tree")
        {
            return Tree(html, html.ViewData.Model, keyPropertyAccessor, contentPropertyAccessor, null, selectedKeys, nullRender, partialViewName);
        }
        /// <summary>
        /// 使用树控件
        /// </summary>
        /// <typeparam name="TModel">模型类型</typeparam>
        /// <typeparam name="TKeyProperty">映射的树中主键类型</typeparam>
        /// <typeparam name="TContentProperty">映射的树中内容的模型属性类型</typeparam>
        /// <param name="html">>HtmlHelper实例</param>
        /// <param name="model">模型对象</param>
        /// <param name="keyPropertyAccessor">Key对应的模型属性访问器</param>
        /// <param name="contentPropertyAccessor">内容对应的模型属性访问性</param>
        /// <param name="contentRenderfunc">树内容呈现方法</param>
        /// <param name="selectedKeys">所选中的Key数组</param>
        /// <param name="nullRender">模型为NULL，呈现方法</param>
        /// <param name="partialViewName">局部视图名称</param>
        /// <returns>>IHtmlString实例</returns>
        public static IHtmlString Tree<TModel, TKeyProperty, TContentProperty>(this HtmlHelper html, TModel model, Expression<Func<TModel, TKeyProperty>> keyPropertyAccessor,
    Expression<Func<TModel, TContentProperty>> contentPropertyAccessor, Func<TContentProperty, object> contentRenderfunc,string[] selectedKeys, Func<object> nullRender = null, string partialViewName = "_Tree")
        {
            var tree = new Tree<TModel, TContentProperty>(model, ExpressionHelper.GetExpressionText(keyPropertyAccessor),
                ExpressionHelper.GetExpressionText(contentPropertyAccessor), contentRenderfunc, selectedKeys);
            return Tree(html, tree, nullRender, partialViewName);
        }
        /// <summary>
        /// 使用树控件
        /// </summary>
        /// <param name="html">HtmlHelper对象实例</param>
        /// <param name="treeModel">TreeNode对象实例</param>
        /// <param name="nullRender">模型为NULL，呈现方法</param>
        /// <param name="partialViewName">局部视图名称</param>
        /// <returns>IHtmlString实例</returns>
        public static IHtmlString Tree(this HtmlHelper html, TreeNode treeModel, Func<object> nullRender = null, string partialViewName = "_Tree")
        {
            if (nullRender == null)
            {
                nullRender = () => string.Empty;
            }
            WidgetDataDictionary viewData = new WidgetDataDictionary<TreeNode>(treeModel, html.ViewData);
            //一定注意，如果起用新的WidgetDataDictionary，一定不能用老的。
            viewData.ModelMetadata.AsWidgetModelMetadata().NullDisplayText = nullRender().ToString();
            //集合类型Model与需显示的属性并不属于同个对象，Model==IEnumerable,properties为集合内的对象属性
            return html.Partial(partialViewName, viewData);

        }
    }
}
